<?php

/**
 * This is the model class for table "eo".
 *
 * The followings are the available columns in table 'eo':
 * @property integer $idEo
 * @property string $numero_eo
 * @property integer $monto_usd
 * @property integer $tasa
 * @property integer $monto_bsf
 * @property string $fecha_deadline
 * @property string $descripcion
 * @property string $nombre
 * @property integer $Customer_id
 *
 * The followings are the available model relations:
 * @property Customer $customer
 * @property Factura[] $facturas
 * @property Ndebito[] $ndebitos
 */
class Eo extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return Eo the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'eo';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('numero_eo, monto_usd, monto_bsf, Customer_id', 'required'),
			array('monto_usd, tasa, monto_bsf, Customer_id', 'numerical', 'integerOnly'=>true),
			array('numero_eo', 'length', 'max'=>25),
			array('nombre', 'length', 'max'=>45),
			array('fecha_deadline, descripcion', 'safe'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('idEo, numero_eo, monto_usd, tasa, monto_bsf, fecha_deadline, descripcion, nombre, Customer_id', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'customer' => array(self::BELONGS_TO, 'Customer', 'Customer_id'),
			'facturas' => array(self::HAS_MANY, 'Factura', 'Eo_id'),
			'ndebitos' => array(self::HAS_MANY, 'Ndebito', 'Eo_idEo'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'idEo' => 'Id Eo',
			'numero_eo' => 'Numero',
			'monto_usd' => 'Monto(USD)',
			'tasa' => 'Tasa',
			'monto_bsf' => 'Monto(VEF)',
			'fecha_deadline' => 'Deadline',
			'descripcion' => 'Descripcion',
			'nombre' => 'Nombre',
			'Customer_id' => 'Customer',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('idEo',$this->idEo);
		$criteria->compare('numero_eo',$this->numero_eo,true);
		$criteria->compare('monto_usd',$this->monto_usd);
		$criteria->compare('tasa',$this->tasa);
		$criteria->compare('monto_bsf',$this->monto_bsf);
		$criteria->compare('fecha_deadline',$this->fecha_deadline,true);
		$criteria->compare('descripcion',$this->descripcion,true);
		$criteria->compare('nombre',$this->nombre,true);
		$criteria->compare('Customer_id',$this->Customer_id);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
        
        public function customer($key=null){
            switch ($key){
                case 'all':
                    $list=array();
                    foreach (Customer::model()->findAll() as $key){
                        $list[$key->idCustomer]=$key->nombre;
                    }
                return $list;
                break;
                default:
                    return Customer::model()->findAllByPk($key?$key:$this->Customer_id);
            }    
            
        }
}